package edu.xidian.sselab.cloudcourse.repository;

import edu.xidian.sselab.cloudcourse.domain.test5_entity;
import edu.xidian.sselab.cloudcourse.domain.test6_entity;
import edu.xidian.sselab.cloudcourse.hbase.HbaseClient;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class test6_Repository {
    private static final String TABLE_NAME = "carSales";
    private final HbaseClient hbaseClient;

    @Autowired
    public test6_Repository(HbaseClient hbaseClient){
        this.hbaseClient = hbaseClient;
    }

    public List<test6_entity> find() throws IOException {
        List<test6_entity> records = new ArrayList<>();
        hbaseClient.getConnection();
        Table table = hbaseClient.getTableByName(TABLE_NAME);
        Scan scan = new Scan();
        ResultScanner rs;
        try {
            if (table != null) {
                rs = table.getScanner(scan);
                for (Result result : rs) {
                    test6_entity tempRecord = new test6_entity().map(result);
                    records.add(tempRecord);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("查询出错，返回一个空的列表!");
        }
        table.close();
        hbaseClient.closeConnection();
        return records;
    }
}
